package java8;

import java.util.*;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

        StreamTest.distinct(numbers);

        StreamTest.sorted(numbers);

        List<String> strList = Arrays.asList("a", "ba", "bb", "abc", "cbb", "bba", "cab");

        StreamTest.filter(strList);
        StreamTest.collect(strList);

        List<StreamObject> streamObjects = new ArrayList();
        streamObjects.add(new StreamObject("2", "李四2"));
        streamObjects.add(new StreamObject("1", "张三"));
        streamObjects.add(new StreamObject("1", "张三1"));
        streamObjects.add(new StreamObject("2", "李四"));

        StreamTest.map(streamObjects);
        StreamTest.peek(streamObjects);

        StreamTest.groupingBy(streamObjects);

        StreamTest.anyMatch(streamObjects);
        StreamTest.findAny(streamObjects);
        StreamTest.findFirst(streamObjects);
//
//        List<Object> collect1 = streamObjects.stream()
//                .sorted(Comparator.comparing(StreamObject::getId)).collect(Collectors.toList());

//        System.out.println(2);
    }

    public static void distinct(List<Integer> list) {
        System.out.println("distinct去重演示========================");
        list = list.stream().distinct().collect(Collectors.toList());
        list.forEach(System.out::println);
    }

    public static void sorted(List<Integer> list) {
        System.out.println("sorted排序演示========================");
        List<Integer> squaresList = list.stream()
                .sorted((x, y) -> y - x)
                .collect(Collectors.toList());

        squaresList.forEach(num -> {
            System.out.println(num);
        });
    }

    public static void groupingBy(List<StreamObject> list) {
        System.out.println("groupby分组演示========================");
        Map collect = list.stream().collect(Collectors.groupingBy(StreamObject::getId));

        collect.forEach((key, value) -> System.out.println(key + "::" + value));
        System.out.println();
    }



    public static void filter(List<String> list) {
        System.out.println("filter过滤演示========================");
        list = list.stream().filter(s -> s.contains("a")).collect(Collectors.toList());
        //测试filter之后空集合是否抛出异常
//        list = list.stream().filter(s -> s.contains("d")).collect(Collectors.toList());

        list.forEach(System.out::println);

    }
    public static void map(List<StreamObject> list) {
        System.out.println("map映射演示========================");
        List<StreamObject> collect = list.stream().map(streamObject -> {
            StreamObject streamObject1 = new StreamObject();
            streamObject1.setName(streamObject.getName() + "2");
            return streamObject1;
        }).collect(Collectors.toList());
        collect.forEach(System.out::println);

    }

    public static void peek(List<StreamObject> list) {
        System.out.println("peek消费演示========================");
        list= list.stream().peek(streamObject -> streamObject.setName(streamObject.getName() + "2")).collect(Collectors.toList());
        list.forEach(System.out::println);
    }

    public static void collect(List<String> list) {
        System.out.println("collect收集演示========================");
        //收集到map中
        Map<Integer, String> strMap = list.stream().collect(Collectors.toMap(str -> list.indexOf(str), str -> str));

        strMap.forEach((key, value) -> {
            System.out.println(key + "::" + value);
        });

        //收集到list中
        List<String> collect = list.stream().collect(Collectors.toList());
        collect.forEach(System.out::println);

    }


    public static void anyMatch(List<StreamObject> list) {
        System.out.println("anyMatch终结操作-集合匹配演示========================");
        boolean b = list.stream().anyMatch(streamObject -> "张三".equals(streamObject.getName()));
        System.out.println(String.format("集合中%s张三", b ? "存在" : "不存在"));

    }

    public static void findAny(List<StreamObject> list) {
        System.out.println("findAny终结操作========================");
        Optional<StreamObject> any = list.stream().findAny();
        System.out.println(String.format("发现%s", any.get().getName()));
    }
    public static void findFirst(List<StreamObject> list) {
        System.out.println("findFirst终结操作========================");
        Optional<StreamObject> any = list.stream().findFirst();
        System.out.println(String.format("发现%s", any.get().getName()));
    }



}

class StreamObject{

    private String id;
    private String name;

    public StreamObject() {
    }

    public StreamObject(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "StreamObject{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
